# -*- coding:utf-8 -*-
# Created by Myth on 7/31/2017.
from bs4 import BeautifulSoup
from parsehtml.course import Course
from parsehtml.student import StudentItem

'''
个人信息
一共五个<tr>  
1. 9个<td>
2. 8 <td>
3. 6 <td>
4. 4 <td>
成绩
<tr height=20 style="height:20px;"></tr>
中的 <tb>标签  如果为12个 <tb>标签，则是课程信息的HTML代码，含有两个课程信息(课程名为空的话，无课程)
由于使用标签数目筛选信息，就存在标签数目符合要求，但是标签内容不符合的现象，所以设置计数变量(比如前两个符合要求)
'''
page1 = BeautifulSoup(open("page1.html"), "lxml")
student = StudentItem()
# 计数变量
four_td_num = 0
eight_td_num = 0
tr_list = page1.find_all(name="tr", attrs={'height': 20, 'style': 'height:20px;'})
for tr in tr_list:
    td_list = tr.find_all(name="td")
    length = len(td_list)
    # 姓名相关信息
    if length == 9:
        student.name = td_list[1].string
        student.no = td_list[3].string
        student.gender = td_list[5].string
        student.id = td_list[7].string
    # 籍贯相关信息
    elif length == 8:
        eight_td_num = eight_td_num + 1
        if eight_td_num < 2:
            student.nation = td_list[1].string
            student.hometown = td_list[3].string
    # 入学时期
    elif length == 6:
        student.period = td_list[3].string
    # 系、专业
    elif length == 4:
        four_td_num = four_td_num + 1
        if four_td_num == 1:
            student.major = td_list[1].string
        if four_td_num == 2:
            student.department = td_list[1].string
    # 成绩
    elif length == 12:
        course_a = Course(td_list[0].string, td_list[1].string, td_list[2].string,
                          td_list[3].string, td_list[4].string, td_list[5].string)
        student.course_list.append(course_a)
        if td_list[6].string is not None:
            course_b = Course(td_list[6].string, td_list[7].string, td_list[8].string,
                              td_list[9].string, td_list[10].string, td_list[11].string)
            student.course_list.append(course_b)

# for course in student.course_list:
#     print course

page2 = BeautifulSoup(open("page2.html"), "lxml")

tr_list = page2.find_all(name="tr", attrs={'height': 20, 'style': 'height:20px;'})
for tr in tr_list:
    td_list = tr.find_all(name="td")
    if len(td_list) == 12:
        course_a = Course(td_list[0].string, td_list[1].string, td_list[2].string,
                          td_list[3].string, td_list[4].string, td_list[5].string)
        student.course_list.append(course_a)
        if td_list[6].string is not None:
            course_b = Course(td_list[6].string, td_list[7].string, td_list[8].string,
                              td_list[9].string, td_list[10].string, td_list[11].string)
            student.course_list.append(course_b)

# for course in student.course_list:
#     print course
print student.calculate_point()
